package com.server.chart;

import com.dao.chart.ChartsConfigMapper;
import com.dao.user.UserMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.model.bo.GroupUpdateId;
import com.model.po.ChartConfig;
import com.model.po.ChartConfigList;
import com.model.pojo.RepCode;
import com.model.pojo.RepEntity;
import com.model.pojo.TestPage;
import com.model.vo.configVo.ChangeOrderInfo;
import com.model.vo.configVo.ChartConfigInfo;
import com.model.vo.configVo.GroupInfo;
import com.server.strategy.StrategysService;
import com.util.GetTokenData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class ChartsConfigService {

    @Autowired
    ChartsConfigMapper chartsConfigMapper;
    @Autowired
    ObjectMapper objectmapper;
    @Autowired
    StrategysService strategysService;
    @Autowired
    GetTokenData getTokenData;
    @Autowired
    UserMapper userMapper;

    /*
    保存表设置
     */
    public RepEntity setChartsConfig(ChartConfigInfo chartConfigInfo, String token){
        Map<String, String> resMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(resMap.get("id"));
        String userName = resMap.get("name");
        ChartConfig chartConfig = new ChartConfig();
        BeanUtils.copyProperties(chartConfigInfo, chartConfig);

        String groupBy = null;
        String accessAuthority = null;
        String updateAuthority = null;

        try {
            groupBy = objectmapper.writeValueAsString(chartConfigInfo.getGroupBy());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        try {
            accessAuthority = objectmapper.writeValueAsString(chartConfigInfo.getAccessAuthority());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        try {
            updateAuthority = objectmapper.writeValueAsString(chartConfigInfo.getUpdateAuthority());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        chartConfig.setGroupBy(groupBy);
        chartConfig.setAccessAuthority(accessAuthority);
        chartConfig.setUpdateAuthority(updateAuthority);
        chartConfig.setCreateId(userId);
        chartConfig.setCreateBy(userName);

        if (chartConfig.getChartsGroup() == 0){
            chartConfig.setChartsGroup(-1);
        }
        if (chartConfig.getChartOption() == null ) {
            chartConfig.setChartOption("");
        }
        if (chartConfig.getFetchConfig() == null || "".equals(chartConfig.getFetchConfig())){
            chartConfig.setFetchConfig("");
        }
        if (chartConfig.getStyle() == null){
            chartConfig.setStyle("");
        }

        chartsConfigMapper.insertCharts(chartConfig);
        int id = chartConfig.getChartId();

        return new RepEntity(RepCode.success,id);
    }

    /*
    更新表设置
     */
    public RepEntity updateChartsConfig(ChartConfigInfo chartConfigInfo, String token){
        Map<String, String> resMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(resMap.get("id"));
        String userName = resMap.get("name");
        int createID = chartsConfigMapper.getCreateId(chartConfigInfo.getChartId());
        if (userId != createID){
            return new RepEntity(RepCode.NoAuthority);
        }
        ChartConfig chartConfig = new ChartConfig();
        BeanUtils.copyProperties(chartConfigInfo, chartConfig);

        String groupBy = null;
        String accessAuthority = null;
        String updateAuthority = null;

        try {
            groupBy = objectmapper.writeValueAsString(chartConfigInfo.getGroupBy());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }


        try {
            accessAuthority = objectmapper.writeValueAsString(chartConfigInfo.getAccessAuthority());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        try {
            updateAuthority = objectmapper.writeValueAsString(chartConfigInfo.getUpdateAuthority());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }


        chartConfig.setGroupBy(groupBy);
        chartConfig.setAccessAuthority(accessAuthority);
        chartConfig.setAccessAuthority(accessAuthority);
        chartConfig.setUpdateAuthority(updateAuthority);
        chartConfig.setCreateId(userId);
        chartConfig.setCreateBy(userName);
        if (chartConfig.getChartsGroup() == 0){
            chartConfig.setChartsGroup(-1);
        }
        if (chartConfig.getChartOption() == null) {
            chartConfig.setChartOption("");
        }
        if (chartConfig.getStyle() == null){
            chartConfig.setStyle("");
        }
        System.out.println("chartOption:"+chartConfig.getChartOption());
        chartsConfigMapper.updateChartConfig(chartConfig);

        return new RepEntity(RepCode.success);
    }

    /*
    移交图表
     */
    public RepEntity changeChartOrder(ChangeOrderInfo changeOrderInfo, String token){
        Map<String, String> resMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(resMap.get("id"));
        String name = userMapper.getName(changeOrderInfo.getUserId());
        int createId = chartsConfigMapper.getCreateId(changeOrderInfo.getId());
        if (createId != userId){
            return new RepEntity(RepCode.NoAuthority);
        }
        chartsConfigMapper.changeChartOrder(name, changeOrderInfo.getUserId(), changeOrderInfo.getId());
        return new RepEntity(RepCode.success);
    }

    /*
    更新图表配置的分组
     */
    public RepEntity updateConfigGroup(GroupUpdateId groupUpdateId){
        int chartId = groupUpdateId.getId();
        int chartGroupId = groupUpdateId.getGroupId();
        chartsConfigMapper.updateConfigGroup(chartId, chartGroupId);
        return new RepEntity(RepCode.success);
    }

    /*
    删除图表
     */
    public RepEntity delChartsConfig(List<Integer> idList){
        chartsConfigMapper.deleteCharts(idList);
        return new RepEntity(RepCode.success);
    }

    /*
    查询图表列表
     */
    public RepEntity getListCharts(String token, TestPage testPage){
        Map<String, String> resMap = getTokenData.getTokenData(token);
        int id = Integer.parseInt(resMap.get("id"));
        List<ChartConfigList> chartsList = chartsConfigMapper.getListByUser(id,testPage.enablePaging());
        PageInfo<ChartConfigList> pageInfo = new PageInfo<>(chartsList);
        return new RepEntity(RepCode.success, pageInfo);
    }

    /*
    查询单个图表配置
     */
    public RepEntity getOneCharts(int id){
        ChartConfig chartConfig = chartsConfigMapper.getOneChart(id);
        if (chartConfig == null){
            return new RepEntity(RepCode.Null);
        }
        String name = chartsConfigMapper.getDataNameById(chartConfig.getDataId());
        if (name == null){
            return new RepEntity(RepCode.DataNonExistent);
        }
        return new RepEntity(RepCode.success, chartConfig);
    }

    /*
    创建图表分组
     */
    public RepEntity setDataConnectorGroup(GroupInfo groupInfo){
        if (groupInfo == null){
            return new RepEntity(RepCode.Null);
        }
        chartsConfigMapper.setChartsGroup(groupInfo);
        int id = groupInfo.getId();
        return new RepEntity(RepCode.success, id);
    }

    /*
    更新图表分组
     */
    public RepEntity updataDataConnectorGroup(GroupInfo groupInfo){
        chartsConfigMapper.updataChartsGroup(groupInfo);
        return new RepEntity(RepCode.success);
    }

    /*
    批量更新图表分组
     */
    public RepEntity updataListGroup(List<GroupInfo> groupInfoList){
        Iterator isList = groupInfoList.iterator();
        GroupInfo groupInfo = new GroupInfo();
        while (isList.hasNext()){
            groupInfo = (GroupInfo) isList.next();
            chartsConfigMapper.updataChartsGroup(groupInfo);
        }

        return new RepEntity(RepCode.success);
    }

    /*
    删除图表分组
     */
    public RepEntity delDataConnectorGroup(List<Integer> idList){
        List<Integer> integerList = idList;
        Iterator isList = integerList.iterator();
        while (isList.hasNext()){
            int id = (int) isList.next();
            List<String> groupName = chartsConfigMapper.getChartFatherId(id);
            if (groupName.size() != 0){
                return new RepEntity(RepCode.hasGroupUsing);
            }
            List<String> chartName = chartsConfigMapper.getChartName(id);
            if (chartName.size() != 0 ){
                return new RepEntity(RepCode.hasChartUsing);
            }
        }
        chartsConfigMapper.delChartsGroup(idList);
        return new RepEntity(RepCode.success);
    }

    /*
    查询图表分组列表
     */
    public RepEntity getConnectorGroup(){
        return new RepEntity(RepCode.success, chartsConfigMapper.getChartsGroup());
    }

}
